package com.siruier.boss.ui.dialog

import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.siruier.boss.R
import com.siruier.boss.common.AGREEMENT
import com.siruier.boss.common.PRIVACY_POLICY
import com.siruier.boss.databinding.DialogSecretBinding
import com.siruier.boss.ui.activity.commom.openUrl
import com.siruier.boss.ui.ext.bindViewClick
import com.siruier.boss.ui.utils.SafeDialogHandle
import com.siruier.boss.ui.utils.ViewSpanUtil
import com.siruier.boss.ui.widget.BpClickableSpan

class SecretDialog(private val mContext: Context) : Dialog(mContext, R.style.dimLoadingdialog), View.OnClickListener {
    var mOnClickListener: ((view: View?) -> Unit)? = null
    private var vb: DialogSecretBinding = DialogSecretBinding.inflate(LayoutInflater.from(context))

    init {
        setContentView(vb.root)
        bindViewClick(this, vb.btnConfirm, vb.btnCancel)
        ViewSpanUtil.Builder(vb.tvSecret, context.getString(R.string.text_secret), context.getString(R.string.text_user_procotol))
            .setClickableSpan(BpClickableSpan(null) { mContext.openUrl(AGREEMENT) })
            .changeSpanText(context.getString(R.string.text_secret_procotol))
            .setClickableSpan(BpClickableSpan(null) { mContext.openUrl(PRIVACY_POLICY) })
            .submit()
        val window = window
        val attributes = window!!.attributes
        attributes.width = (context.resources.displayMetrics.widthPixels * 0.8).toInt()
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT
        window.attributes = attributes
        setCancelable(false)
    }

    override fun onClick(view: View?) {
        SafeDialogHandle.safeDismissDialog(this)
        mOnClickListener?.invoke(view)
    }
}